//
//  SubjectDetailPageVC.m
//  ErWa
//
//  Created by 赵颜彬 on 16/3/10.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SubjectDetailPageVC.h"
#import "SubjectDetailPageModel.h"
#import "AFNetworking.h"
#import <UIImageView+WebCache.h>
#import "BaseView.h"
#import "AccountManager.h"
#import "NewNestHeader.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface SubjectDetailPageVC ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) NSMutableDictionary *dataDic;
@property (nonatomic, strong) NSMutableArray *imgArr;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UIScrollView *scrollview;
@property (nonatomic, strong) UIButton *likeButton;
@end

@implementation SubjectDetailPageVC
// 页面出现时判断是否已经收藏 确定收藏按钮的状态
- (void)viewWillAppear:(BOOL)animated
{
    
    //解档获取数据
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    //2.拼接地址 (按照什么路径 保存为什么文件) (数组和字典写plist)
    NSString *strpath = [path stringByAppendingPathComponent:@"a.plist"];
    
    NSString *str = [NSKeyedUnarchiver unarchiveObjectWithFile:strpath];
    UIColor *bg = [UIColor colorWithPatternImage:[UIImage imageNamed:str]];
    //     判断当前是什么模式
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        
        
        self.view.backgroundColor = [UIColor changeColorWithString:@"#a1a3a6"];
        
    } else {
        self.view.backgroundColor = [UIColor clearColor];
        [self.view setBackgroundColor:bg];
        //导航栏按钮颜色
        self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:str]];
        //Tabbar按钮颜色
        self.tabBarController.tabBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:str]];
        
    }

    
    
    
    
    
    
    
    
    
    
    // 判断是否已经登录
    //    BOOL login = [[NSUserDefaults standardUserDefaults] boolForKey:@"didLogin"];
    //    if (login) {
    // 如果登录了 获取当前保存的专题的数组并且遍历 确定是不是已经收藏过
    //获取文件夹的路径
    NSString *at = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"Subject"];
    //获取当前用户名
    NSString *nameStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    NSString *namePath = [at stringByAppendingPathComponent:nameStr];
    NSString *detailPath = [namePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@Modal.plist", @"Subject"]];
    // 获取数组
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:detailPath];
    // 遍历数组
    for (SubjectModel *m in arr) {
        // 如果题目相等
        if ([m.title isEqualToString:_mainModal.title]) {
            //将喜欢按钮的状态置为红色
            _likeButton.selected = YES;
        }
    }
    //    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"专题详情";

    [self dataHandler];
    // 判断当前是什么模式
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        self.view.backgroundColor = [UIColor grayColor];
    } else {
        self.view.backgroundColor = [UIColor whiteColor];
        
    }
   }

/// 处理数据
- (void)dataHandler
{
    NSString *urlStr = [NSString stringWithFormat:@"http://appapi.17house.com/xiaoguotuApi.php?action=albumDetail&id=%@", _idStr];
    [AFNetworking GETWithURL:urlStr completion:^(id result) {
        self.dataDic = result[@"data"];
        
        self.imgArr = _dataDic[@"images"];
        
        
        // 视图创建 标题和内容的
        [self createSubviews];
        _titleLabel.text = _dataDic[@"name"];
        _contentLabel.text = _dataDic[@"desc"];
        

        // 遍历图片数组
        for (NSInteger i = 0; i < _imgArr.count; i++) {
            // 创建字典
            NSDictionary *dic = _imgArr[i];
            //如果like_desc不为空,就创建UIView(有内容和图片) 如果为空就创建UIImageView(仅图片)
            if ([dic[@"like_desc"] isEqualToString:@""])  {
                UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(10, HEIGHT / 2.5 * (i + 1), WIDTH - 20, HEIGHT / 2.5)];
                [imgview sd_setImageWithURL:[NSURL URLWithString:dic[@"path"]] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
                [self.scrollview addSubview:imgview];
            }else{
                BaseView *Bview = [[BaseView alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height / 2.5 * (i + 1), self.view.frame.size.width - 20, self.view.frame.size.height/ 2.5)];
                [Bview.mainImg sd_setImageWithURL:[NSURL URLWithString:dic[@"path"]] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
                Bview.titleLabel.text = dic[@"like_desc"];
                [self.scrollview addSubview:Bview];
            }
        }
        
    }];
    
}
/// 视图创建
- (void)createSubviews
{
    //滚动视图
    self.scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64)];
    [self.view addSubview:_scrollview];
    self.scrollview.contentSize = CGSizeMake(WIDTH, HEIGHT / 2.5 * (_imgArr.count + 1));
    _scrollview.contentInset = UIEdgeInsetsMake(0, 0, 44, 0);
    
    // 标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, WIDTH - 20, HEIGHT / 20)];
    [self.scrollview addSubview:_titleLabel];
    _titleLabel.font = [UIFont systemFontOfSize:19];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    
    //内容
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20 + _titleLabel.frame.size.height, self.view.frame.size.width - 20, self.view.frame.size.height / 3.5)];
    [self.scrollview addSubview:_contentLabel];
    _contentLabel.font = [UIFont systemFontOfSize:15];
    _contentLabel.numberOfLines = 0;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
